//获取应用实例
const app = getApp();

Page({
	data: {
    list: []
  },
	onLoad: function() {
    
  },

  onShow:function(){
      this.getAddressList();
  },


  address(){
    wx.navigateTo({
      url: 'setting-add_address',
    })
  },

//获取收货地址列表
  getAddressList(){
    var that=this;
    wx.request({
      url: app.globalData.api + '/app.php?c=ConsigneeAddress&a=getAddressList',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        'token': '' + app.globalData.token
      },
      success: (res) => {
        console.log(res)
        that.setData({
          list:res.data.data.list
        })
      },
    });
  },

  edit_address:function(e){
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: 'setting-add_address?id=' + e.currentTarget.dataset.id,
    })
  },

  //修改默认收获地址
  select:function(e){
    console.log(e)
    var ids=e.currentTarget.dataset.id
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/ConsigneeAddress/changeDefault',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        token: '' + app.globalData.token,
        id: ids,
      },
      dataType: 'JSON',
      success: (res) => {
        console.log(res)
        var datas=JSON.parse(res.data)
        if (datas.code==1){
          wx.showToast({
            title: '已设为默认地址',
            icon: 'success'
          })
          setTimeout(function () {
            //要延时执行的代码
            wx.navigateBack({
              delta: 1
            })
          }, 1000) //延迟时间 这里是1秒
        }else{
          wx.showToast({
            title: '' + datas.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
      },
    });
  },
 
  deleteList:function(e){
    console.log(e.currentTarget.id)
    var that=this;
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗？',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.api + '/app.php?c=ConsigneeAddress&a=delAddress',
            method: 'post',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
              'platform': '' + app.globalData.platform,
              'version': '' + app.globalData.version,
            },
            data: {
              token: '' + app.globalData.token,
              address_id: e.currentTarget.id,
            },
            success: (res) => {
              console.log(res)
              if (res.data.code == 0) {
                that.getAddressList();
                wx.showToast({
                  title: '删除成功',//提示文字
                  duration: 2000,//显示时长
                  mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                  icon: 'success', //图标，支持"success"、"loading"  
                  success: function () { },//接口调用成功
                  fail: function () { },  //接口调用失败的回调函数  
                  complete: function () { } //接口调用结束的回调函数  
                })
              
              } else {
                wx.showToast({
                  title: '' + res.data.msg,//提示文字
                  duration: 2000,//显示时长
                  mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                  icon: 'none', //图标，支持"success"、"loading"  
                  success: function () { },//接口调用成功
                  fail: function () { },  //接口调用失败的回调函数  
                  complete: function () { } //接口调用结束的回调函数  
                })
              }
            },
          });
        } else if (sm.cancel) {

        }
      }
    })
  },
});
